package com.up.vms.application.service;

import com.up.vms.application.common.DomainUtils;
import com.up.vms.domain.module.BannerDTO;
import com.up.vms.domain.vm.entity.Banner;
import com.up.vms.domain.vm.service.BannerService;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BannerApplicationService {

    @Autowired
    private BannerService bannerService;

    public GeneRespDTO<HashMap> getBannerList(BannerDTO bannerDTO) throws Exception {
        GeneRespDTO geneRespDTO = null;

        Page<Banner> simpleBanners = null;
        if (CommonUtils.isNotNull(bannerDTO.getBannerName())) {
            simpleBanners = bannerService.findAllByBannerName(bannerDTO);
        } else if (CommonUtils.isNotNull(bannerDTO.getStatus())) {
            simpleBanners = bannerService.findAllByStatus(bannerDTO);
        } else {
            simpleBanners = bannerService.findAllByPageable(bannerDTO);
        }

        if (CommonUtils.isNotNull(simpleBanners)) {
            geneRespDTO = DomainUtils.convert(simpleBanners);
        } else {
            throw new Exception("没有记录");
        }
        return geneRespDTO;
    }

    public GeneRespDTO<HashMap> saveBanner(Banner banner) throws Exception {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        Banner saveBanner = null;
        if (CommonUtils.isNotNull(banner.getBannerId())) {
            saveBanner = bannerService.findBannerByBannerId(banner.getBannerId());
            if (CommonUtils.isNotNull(saveBanner)) {
                saveBanner.setBannerName(banner.getBannerName());
                saveBanner.setStatus(banner.getStatus());
                saveBanner.setSorting(banner.getSorting());
                saveBanner.setBannerImage(banner.getBannerImage());
                saveBanner.setForwardType(banner.getForwardType());
                saveBanner.setForwardUrl(banner.getForwardUrl());
                saveBanner.setModified(new Date());
            } else {
                saveBanner = banner;
                saveBanner.setBannerId(null);
            }
        } else {
            saveBanner = banner;
        }

        Banner newBanner = bannerService.save(saveBanner);

        geneRespDTO.setData(newBanner);
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));

        return geneRespDTO;
    }

    public GeneRespDTO delete(List<String> bannerIdList) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        for (String idStr : bannerIdList) {
            Long bannerId = Long.parseLong(idStr);
            Banner banner = bannerService.findBannerByBannerId(bannerId);
            if (CommonUtils.isNotNull(banner)) {
                bannerService.delete(banner);
            }
        }
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(bannerIdList);
        return geneRespDTO;
    }
}
